//
//  HomeViewController.m
//  TextApplication
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "HomeViewController.h"
#import "ZWYNetTool.h"
#import "CycleImage.h"
#import "TextView.h"
#import "TAMacro.h"
#import "HomeCycleModel.h"
#import "CycleDetailViewController.h"

#import "MJRefresh.h"
#import "MBProgressHUD.h"


@interface HomeViewController () <SendUrlStr, SendImageUrl, UIScrollViewDelegate> // 签协议
@property (nonatomic, strong) NSMutableArray *picArray;
@property (nonatomic, strong) UIScrollView *backScrollView;
@property (nonatomic, strong) NSMutableArray *cycleImageArray;
@property (nonatomic, strong) CycleImage *cycleImage;
@property (nonatomic, strong) NSMutableArray *fourPicArray;
@property (nonatomic, strong) NSMutableArray *urlArray;
@property (nonatomic, strong) NSMutableArray *imageUrlArray;

@property (nonatomic, copy) NSString *urlStr;

@property (nonatomic, strong) TextView *text;
@end

@implementation HomeViewController

// 实现协议方法
- (void)sendUrlStr:(NSString *)urlStr {
    CycleDetailViewController *detail = [[CycleDetailViewController alloc] init];
    detail.urlStr = urlStr;
    [self.navigationController pushViewController:detail animated:YES];

}
- (void)sendUrl:(NSString *)url {
    CycleDetailViewController *detailVC = [[CycleDetailViewController alloc] init];
    detailVC.urlStr = url;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 创建轮播图

    [self createWaterView];
    self.cycleImage = [[CycleImage alloc] initWithFrame:CGRectMake(0, 0, self.backScrollView.bounds.size.width, 240)];
    [self.backScrollView addSubview:self.cycleImage];
    self.cycleImage.delegate = self; // 设置代理人

    [self createData]; // 请求轮播图数据
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.backScrollView.contentOffset.y < 0) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");
        [hud hideAnimated:YES afterDelay:1];
        NSLog(@"下拉刷新");
        [self createData];
    }
}

- (void)createWaterView {
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 99, WIDTH, HEIGHT - 99 - 44)];
    self.backScrollView.delegate = self;
    self.backScrollView.contentSize = CGSizeMake(0, WIDTH * 2 + 50 + 240);
    self.backScrollView.backgroundColor = [UIColor colorWithWhite:0.176 alpha:1.000];
    UIView *aView = [[UIView alloc] init];
    aView.backgroundColor = [UIColor redColor];
    self.text = [TextView new];
    self.text.delegate = self; // 签协议
    aView = [self.text  createViewFrame:CGRectMake(0, 0, WIDTH, WIDTH * 3 / 2 + 480)];
    [self.backScrollView addSubview:aView];
    [self.view addSubview:self.backScrollView];

}

- (void)createData {
    self.cycleImageArray = [NSMutableArray array];
    self.fourPicArray = [NSMutableArray array];
    self.urlArray = [NSMutableArray array];
    self.imageUrlArray = [NSMutableArray array];
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Proxy-Connection": @"keep-alive", @"Accept": @"*/*", @"Accept-Language": @"zh-Hans-CN;q=1, en-CN;q=0.9", @"Connection": @"keep-alive", @"User-Agent": @"iLiangCang/3.0.0 (iPhone; iOS 9.2.1; Scale/2.00) Paros/3.2.13"};
    [ZWYNetTool GET:@"http://mobile.iliangcang.com/goods/shopHome?a=b&app_key=iPhone&v=+r&sig=254F2CCC-7E43-4833-A27D-7D12702F5981&user_token=03723b5fa4100c1079e788f94d65908d" andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSArray *cycle = result[@"data"][@"items"][@"slide"];
        for (NSDictionary *dic in cycle) {
            HomeCycleModel *cycleModel = [HomeCycleModel modelWithDic:dic];
            NSString *picStr = cycleModel.pic_url;
            NSString *urlStr = cycleModel.topic_url;
            [self.cycleImageArray addObject:picStr];
            [self.urlArray addObject:urlStr];
        }
        NSString *first = [self.cycleImageArray firstObject];
        NSString *last = [self.cycleImageArray lastObject];
        [self.cycleImageArray insertObject:last atIndex:0];
        [self.cycleImageArray addObject:first];
        self.cycleImage.array = self.cycleImageArray;
        self.cycleImage.urlSendArray = self.urlArray;

        NSArray *imageArray = result[@"data"][@"items"][@"list"];
        NSString *picStr1 = imageArray[0][@"one"][@"pic_url"];
        NSString *picStr2 = imageArray[0][@"two"][@"pic_url"];
        NSString *picStr3 = imageArray[0][@"three"][@"pic_url"];
        NSString *picStr4 = imageArray[0][@"four"][@"pic_url"];
        NSString *picStr5 = imageArray[1][@"one"][@"pic_url"];
        NSString *picStr6 = imageArray[2][@"one"][@"pic_url"];
        NSString *picStr7 = imageArray[2][@"two"][@"pic_url"];
        
        NSArray *seven = @[picStr1, picStr2, picStr3, picStr4, picStr5, picStr6, picStr7];
        self.fourPicArray = [NSMutableArray arrayWithArray:seven];
        self.text.imageViewArray = self.fourPicArray;
        
        NSString *picUrl1 = imageArray[0][@"one"][@"topic_url"];
        NSString *picUrl2 = imageArray[0][@"two"][@"topic_url"];
        NSString *picUrl3 = imageArray[0][@"three"][@"topic_url"];
        NSString *picUrl4 = imageArray[0][@"four"][@"topic_url"];
        NSString *picUrl5 = imageArray[1][@"one"][@"topic_url"];
        NSString *picUrl6 = imageArray[2][@"one"][@"topic_url"];
        NSString *picUrl7 = imageArray[2][@"two"][@"topic_url"];
        
        NSArray *url7 = @[picUrl1, picUrl3, picUrl2, picUrl4, picUrl5, picUrl6, picUrl7];
        self.imageUrlArray = [NSMutableArray arrayWithArray:url7];
        self.text.urlArr = self.imageUrlArray;
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
